package com.example.controller;

import com.example.dto.dict.Dict;
import com.example.dto.dict.DictCriteria;
import com.example.exception.BadRequestException;
import com.example.server.DictServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：字典管理")
@RequestMapping("/api/dict")
public class DictController {

    @Autowired
    private DictServer dictServer;
    private static final String ENTITY_NAME = "dict";

    @ApiOperation("新增字典")
    @PostMapping
    public ResponseEntity<Object> createDict(@Validated @RequestBody Dict resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        dictServer.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("删除字典")
    @DeleteMapping
    public ResponseEntity<Object> deleteDict(@RequestBody Set<Long> ids){
        dictServer.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("修改字典")
    @PutMapping
    public ResponseEntity<Object> updateDict(@RequestBody Dict resources){
        dictServer.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("查询字典")
    @GetMapping
    public Map<String, Object> list (DictCriteria dictCriteria) {
        Map<String, Object> result = new HashMap<>(4);
        result=dictServer.list(dictCriteria);
        return result;
    }

    @ApiOperation("查询字典")
    @GetMapping(value = "/all")
    public ResponseEntity<Object> queryAllDict(){
        return new ResponseEntity<>(dictServer.findAll(new Dict()),HttpStatus.OK);
    }
}
